package com.wnhz.work.jy.utils;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.jy.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者 zxb:
 * @date 创建时间：2016年7月1日
 * @Description
 */

public class AddImages {

    public static ImageOptions banneroptions = new ImageOptions.Builder()
            //设置加载过程中的图片
            .setLoadingDrawableId(R.mipmap.img_bannerzhanweitu)
            //设置加载失败后的图片
            .setFailureDrawableId(R.mipmap.img_bannerzhanweitu)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(false)
            //设置支持gif
            .setIgnoreGif(false)
            .build();

    /**
     * 模拟填充轮播图数据
     * img_bannerzhanweitu
     *
     * @param context
     * @param autoPlayImages
     * @param onClickListener
     */
    public static ArrayList<View> addImageDatas(Context context, List<String> autoPlayImages,
                                                OnClickListener onClickListener) {

        ArrayList<View> viewList = new ArrayList<>();
        if (autoPlayImages.size() > 1) {
            ImageView imageFirst = new ImageView(context);
            imageFirst.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            ImageLoader.getInstance().displayImage(autoPlayImages.get(autoPlayImages.size() - 1), imageFirst);
            imageFirst.setScaleType(ScaleType.FIT_XY);

            imageFirst.setTag(autoPlayImages.size() - 1);
            imageFirst.setOnClickListener(onClickListener);
            viewList.add(imageFirst);

            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setScaleType(ScaleType.CENTER_CROP);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setScaleType(ScaleType.FIT_XY);

                ImageLoader.getInstance().displayImage(autoPlayImages.get(i), image);
                image.setTag(i);
                image.setOnClickListener(onClickListener);
//			image.setOnClickListener(onClickListener);

                viewList.add(image);
            }

            ImageView imageLast = new ImageView(context);
            ImageLoader.getInstance().displayImage(autoPlayImages.get(0), imageLast);
            imageLast.setScaleType(ScaleType.FIT_XY);
            imageLast.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            imageLast.setTag(0);
            imageLast.setOnClickListener(onClickListener);
            viewList.add(imageLast);
        } else {
            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setAdjustViewBounds(true);
                ImageLoader.getInstance().displayImage(autoPlayImages.get(i), image);
                //x.image().bind(image,autoPlayImages.get(i),banneroptions);
                viewList.add(image);
            }
        }


        return viewList;
        // viewPager.setAdapter(new MyViewPagerAdapter(viewList));

    }


    public static ArrayList<View> addImageDatas(Context context, List<String> autoPlayImages) {
        ArrayList<View> viewList = new ArrayList<>();


        if (autoPlayImages.size() > 1) {
            ImageView imageFirst = new ImageView(context);
            imageFirst.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            ImageLoader.getInstance().displayImage(autoPlayImages.get(autoPlayImages.size() - 1), imageFirst);
            //x.image().bind(imageFirst,autoPlayImages.get(autoPlayImages.size()-1),banneroptions);
            imageFirst.setAdjustViewBounds(true);

            viewList.add(imageFirst);

            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setAdjustViewBounds(true);

                ImageLoader.getInstance().displayImage(autoPlayImages.get(i), image);
                //x.image().bind(image,autoPlayImages.get(i),banneroptions);
                viewList.add(image);
            }

            ImageView imageLast = new ImageView(context);
            ImageLoader.getInstance().displayImage(autoPlayImages.get(0), imageLast);
            //x.image().bind(imageLast,autoPlayImages.get(0),banneroptions);
            imageLast.setAdjustViewBounds(true);
            imageLast.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            viewList.add(imageLast);
        } else {
            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setAdjustViewBounds(true);
                ImageLoader.getInstance().displayImage(autoPlayImages.get(i), image);
                //x.image().bind(image,autoPlayImages.get(i),banneroptions);
                viewList.add(image);
            }
        }


        return viewList;
        // viewPager.setAdapter(new MyViewPagerAdapter(viewList));

    }

    public static ArrayList<View> addImageDatas(Context context, List<String> autoPlayImages, ImageOptions options) {
        ArrayList<View> viewList = new ArrayList<>();


        if (autoPlayImages.size() > 1) {
            ImageView imageFirst = new ImageView(context);
            imageFirst.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            //ImageLoader.getInstance().displayImage(autoPlayImages.get(autoPlayImages.size() - 1),imageFirst);
            x.image().bind(imageFirst, autoPlayImages.get(autoPlayImages.size() - 1), options);
            imageFirst.setAdjustViewBounds(true);

            viewList.add(imageFirst);

            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setAdjustViewBounds(true);

                //ImageLoader.getInstance().displayImage(autoPlayImages.get(i),image);
                x.image().bind(image, autoPlayImages.get(i), options);
                viewList.add(image);
            }

            ImageView imageLast = new ImageView(context);
            ImageLoader.getInstance().displayImage(autoPlayImages.get(0), imageLast);
            x.image().bind(imageLast, autoPlayImages.get(0), options);
            //imageLast.setAdjustViewBounds(true);
            imageLast.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            viewList.add(imageLast);
        } else {
            for (int i = 0; i < autoPlayImages.size(); i++) {
                ImageView image = new ImageView(context);
                image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                image.setAdjustViewBounds(true);
                //ImageLoader.getInstance().displayImage(autoPlayImages.get(i),image);
                x.image().bind(image, autoPlayImages.get(i), options);
                viewList.add(image);
            }
        }


        return viewList;
        // viewPager.setAdapter(new MyViewPagerAdapter(viewList));

    }
}
